import { Vec3 } from "cc";

import { BaseData } from "../../framework/base/BaseData";
import { EventMgr } from "../../framework/manager/EventMgr";
import { TimeUtils } from "../../framework/utils/TimeUtils";
import { EventName, GameConfig } from "../config/Config";

class Data {
    /** 新增：最后体力恢复时间戳 */
    lastHpTime: number = 0;
    /** 下次恢复剩余时间（秒） */
    nextRecoverySec: number = 0;
    /**体力 */
    roleHp: number = 0;
    /**最大体力 */
    maxRoleHp: number = 5;
    /**是否打开震动 */
    isOpenShark: boolean = true;
    /**是否登陆过 false没登陆过 */
    inGame: boolean = false;
    /**是否新用户 */
    isNewUser: boolean = false;
    /**技能id 铁铲4 调色5 冻结6 刷新7 魔法棒8 加时9 翻倍星星10 */
    skill1: number = 0
    skill2: number = 0
    skill3: number = 1
    //体力恢复1点的结束时间
    addHpEndTime: number = 600

    //当前宝箱结束时间
    onlyOverTime: number = 0
    //当前登录时间
    onlyTime: number = 0

    //今日已分享
    dayShare: boolean = false
    //今日已游戏圈
    dayCircle: boolean = false
    //玩家是否一訂閲
    roleDY: boolean = false

    /**积分分数 */
    score: number = 0;
    /**历史最大分数 */
    hisMaxScore: number = 0;
    /**历史最大连击 */
    hisMaxCombo: number = 0;
    /**历史最大连胜 */
    hisMaxWin: number = 0;
    /**解锁段位状态 0-青铜，1-白银，2-黄金*/
    unlockRank: number[] = [0, -1, -1, 0,-1]

    showTipLevelOnes: number[] = [0, -1, -1, 0, 0];

    /**当前段位 */
    curRank: number = 0;
    levels: number[] = [1, 1, 1, 1, 1];
    /**是否无限体力-观看获取体力视频3次默认无限体力，第二天同时刻之后清除无限体力状态 */
    isInfiniteHp: number = 0;
    /** 无限体力结束时间戳 */
    unlimitedHpEndTime: number = 0;
    /** 当日观看体力视频次数 */
    videoWatchedCount: number = 0;

    isNewGuide: boolean = true;

    /** 
     * 每日领取的道具奖励
     *@param 0-提示
     *@param 1-敲碎
     *@param 2-刷新
     */
    dayGifts: number[] = [0, 0, 0];
}

export class UserData extends BaseData {
    protected ecrypt: boolean = false;
    data: Data;
    gameKey: string = GameConfig.AppCacheName + "udata";
    private isNewUser: boolean = false;


    public createData(): Data {
        //这里先读取服务器数据
        this.data = new Data();
        return this.data;
    }
    public initData(isCreatre: boolean): void {
        let _data = new Data();
        for (let key in _data) {
            if (this.data[key] == undefined) {
                this.data[key] = _data[key]
            }
        }
        if (isCreatre) {
            this.isNewUser = true;
        }
        if (this.isNewUser) {
            this.setIsNewUser(true)
        }
        this.saveData()
    }
    setIsNewUser(value: boolean) {
        this.data.isNewUser = value;
        this.saveData()
    }
    getIsNewUsers() {
        if (this.data.isNewUser == undefined) {
            this.data.isNewUser = false;
        }
        return this.data.isNewUser;
    }
    //-========================福利beg
    /**获取今日是否已訂閲 */
    public getDayDY(): boolean {
        if (this.data.roleDY == undefined) {
            this.data.roleDY = false;
        }
        return this.data.roleDY;
    }
    /**以訂閲 */
    public SetDayDY(sha: boolean) {
        this.data.roleDY = sha
        this.saveData();
    }

    /**获取今日是否已分享 */
    public getDayShare(): boolean {
        if (this.data.dayShare == undefined) {
            this.data.dayShare = false;
        }
        return this.data.dayShare;
    }

    /**设置今日是否已分享 */
    public SetDayShare(sha: boolean) {
        this.data.dayShare = sha
        this.saveData();
    }
    /**获取今日是否已游戏圈 */
    public getDaydayCircle(): boolean {
        if (this.data.dayCircle == undefined) {
            this.data.dayCircle = false;
        }
        return this.data.dayCircle;
    }

    /**设置今日是否已游戏圈 */
    public SetDayCircle(sha: boolean) {
        this.data.dayCircle = sha
        this.saveData();
    }

    //-========================福利end
    /**获取是否是新用户 */
    public getIsNewUser(): boolean {
        return this.isNewUser
    }

    //---------------------在线宝箱    onlyTime
    /**获取登录时间 */
    public getonlyTime(): number {
        if (this.data.onlyTime == undefined) {
            this.data.onlyTime = 0;
        }
        return this.data.onlyTime;
    }
    /**设置登录时间 */
    public setonlyTime(num: number,) {
        if ((num - this.getonlyTime()) > 900) {
            let addHp = (num - this.getonlyTime()) / 900
            this.getRoleHp() + addHp > 5 ? this.setRoleHp(5) : this.addRoleHp(Math.ceil(addHp))
            if (this.getRoleHp() < 5) {
                this.setHpAddTime(num + 900)
            }
        }
        this.data.onlyTime = num;
        this.saveData();
    }
    public getonlyOverTime(): number {
        if (this.data.onlyOverTime == undefined) {
            this.data.onlyOverTime = 0;
        }
        return this.data.onlyOverTime;
    }
    //---------------------
    public setinGame(): void {
        this.data.inGame = true;
        this.saveData();
    }
    /**获取是否是新用户 */
    public getinGame(): boolean {
        return this.data.inGame
    }
    //------------------------体力
    /**体力 */
    public getRoleHp(): number {
        return this.data.roleHp;
    }

    /**添加体力 */
    public addRoleHp(num: number, pos?: Vec3) {
        this.data.roleHp += num;
        this.saveData();
        EventMgr.getInstance().emit(EventName.RefreshHP, this.data.roleHp, num, pos);
    }
    /**消耗体力 */
    public subRoleHpNum(num: number): boolean {
        if (this.data.roleHp < num) {
            return false;
        }
        this.data.roleHp -= num;
        // EventMgr.getInstance().emit(EventName.RefreshGold, false, num);
        this.saveData();
        return true;
    }
    /**设置体力回复1点的结束时间 */
    public setHpAddTime(endTime: number) {
        this.data.addHpEndTime = endTime
        this.saveData()
    }
    /**获取体力回复1点的结束时间 */
    public getHpAddTime() {
        if (this.data.addHpEndTime == 0)
            return TimeUtils.GetTimeBySecond()
        return this.data.addHpEndTime
    }
    // /**登录检测 */
    // public setHpAddMaxTime() {
    //     this.data.maxHpEndTime += PlayerMgr.getInstance().timeAdd
    // }
    //------------------------技能
    /**技能id */
    public getSkillById(id: number): number {
        switch (id) {
            case 1:
                return this.data.skill1;
            case 2:
                return this.data.skill2;
            case 3:
                return this.data.skill3;
            default:
                break;
        }

    }
    /**添加技能数 */
    public addSkillNum(num: number, id: number) {
        switch (id) {
            case 1:
                this.data.skill1 += num;
                this.saveData();
                return
            case 2:
                this.data.skill2 += num;
                this.saveData();
                return
            case 3:
                this.data.skill3 += num;
                this.saveData();
                return
            default:
                break;
        }
    }
    /**消耗技能 */
    public subSkillNum(num: number, id: number): boolean {
        switch (id) {
            case 1:
                if (this.data.skill1 < num) {
                    return false;
                }
                this.data.skill1 -= num;
                this.saveData();
                return true
            case 2:
                if (this.data.skill2 < num) {
                    return false;
                }
                this.data.skill2 -= num;
                this.saveData();
                return true
            case 3:
                if (this.data.skill3 < num) {
                    return false;
                }
                this.data.skill3 -= num;
                this.saveData();
                return true
            default:
                break;
        }
    }

    //------------------------积分
    /**获取积分 */
    public get gameScore(): number {
        return this.data.score;
    }
    /**设置积分 */
    public set gameScore(num: number) {
        this.data.score = num;
        this.saveData();
    }

    /**获取震动 */
    getShark() {
        if (this.data.isOpenShark == undefined) {
            this.data.isOpenShark = true;
        }
        return this.data.isOpenShark;
    }
    /**设置震动 */
    setShark(isOpen: boolean) {
        if (this.data.isOpenShark == undefined) {
            this.data.isOpenShark = true;
        }
        this.data.isOpenShark = isOpen;
        this.saveData();
    }
    //--------------------历史之最
    /**获取历史最大分数 */
    public getHisMaxScore(): number {
        return this.data.hisMaxScore;
    }
    /**判断是否历史之最--历史最大分数 */
    public setHisMaxScore(num: number) {
        if (this.data.hisMaxScore >= num) {
            return false
        }
        this.data.hisMaxScore = num;
        this.saveData();
        return true
    }
    /**
     * 获取历史最大连击
     */
    public getHisMaxCombo(): number {
        return this.data.hisMaxCombo;
    }
    /**
     *  设置历史最大连击
     * @param num 
     * @returns 
     */
    public setHisMaxCombo(num: number) {
        if (this.data.hisMaxCombo >= num) {
            return false
        }
        this.data.hisMaxCombo = num;
        this.saveData();
        return true
    }
    /**
     * 获取历史最大连胜
     */
    public getHisMaxWin(): number {
        return this.data.hisMaxWin;
    }
    /**
     * 设置历史最大连胜
     * @param num
     * @returns 
     * */
    public setHisMaxWin(num: number) {
        if (this.data.hisMaxWin >= num) {
            return false
        }
        this.data.hisMaxWin = num;
        this.saveData();
        return true
    }
    /**获取段位解锁 */
    public getUnlockRank(index: number): boolean {
        return this.data.unlockRank[index] >= 0
    }

    /**
     * 设置段位解锁
     * @param num 0-青铜 1-白银 2-黄金
     * @returns
    */
    public setUnlockRank(num: number) {
        this.data.unlockRank[num] = 0;
        this.saveData();
    }
    /**设置段位解锁 */

    /**
     * 升级 
     * @param gameType 游戏类型 1-冒险 2-闯关 3-无尽 4-竞速 5-挑战
     * 
     */
    uplevel(gameType: number) {
        this.data.levels[gameType]++
        this.saveData();
    }
    getLevel(gameType: number = 0) {
        return this.data.levels[gameType];
    }



    // 修改原有体力设置方法
    setRoleHp(num: number) {
        this.data.roleHp = num;
        // 当体力变化时重置计时
        // if (num < 5 && !this.data.lastHpTime) {
        //     this.data.lastHpTime = Math.floor(Date.now() / 1000);
        //     this.startHpRecovery();
        // }
        this.saveData();
        EventMgr.getInstance().emit(EventName.RefreshHP, this.data.roleHp, num);
    }



} 